/*
 * MegaMek -
 * Copyright (C) 2007 Ben Mazur (bmazur@sev.org)
 * 
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 */
package megamek.client.bot;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;

import megamek.common.Coords;
import megamek.common.Entity;
import megamek.common.Player;
import megamek.common.event.GameEvent;
import megamek.common.event.GamePlayerChatEvent;

// TODO: Auto-generated Javadoc
/**
 * The Class ChatProcessor.
 */
public class ChatProcessor {

    /**
     * Process chat.
     *
     * @param gameEvent the ge
     * @param testBot the tb
     */
    protected final void processChat(final GamePlayerChatEvent gameEvent, final TestBot testBot) {
        if (gameEvent.getType() != GameEvent.GAME_PLAYER_CHAT) {
            return;}
        if (testBot.getLocalPlayer() == null) {
            return;}
        final String message = gameEvent.getMessage();
        if (message.contains("declares individual victory at the end of the turn.")
                || message.contains("declares team victory at the end of the turn.")) {
            final String name = message.split(" ")[1];
            for(Player person:testBot.game.getPlayersVector()) {
                if(person.getName().equals(name)) {
                    if(person.isEnemyOf(testBot.getLocalPlayer())) {
                        testBot.sendChat("/defeat");
                    } 
                    break;
                }
            }
            return;
        }
        if (message.contains("type /victory to accept the surrender")) {
            final String name = message.split(" ")[1];
            for(Player p:testBot.game.getPlayersVector()) {
                if(p.getName().equals(name)) {
                    if(p.isEnemyOf(testBot.getLocalPlayer())) {
                        testBot.sendChat("/victory");
                    }
                    break;
                }
            }
            return;
        }
        final StringTokenizer stringToken = new StringTokenizer(gameEvent.getMessage(), ":"); //$NON-NLS-1$
        if (!stringToken.hasMoreTokens()) {
            return;
        }
        final String name = stringToken.nextToken().trim();
        // who is the message from?
       final Enumeration<Player> enumPlayer = testBot.game.getPlayers(); // NOPMD by Adj1031 on 5/18/14 11:56 AM
        Player person = null;
        while (enumPlayer.hasMoreElements()) {
            person = enumPlayer.nextElement();
            if (name.equalsIgnoreCase(person.getName())) {
                break;
            }
        }
        if (person == null) {
            return;
        }
        try {
            if (stringToken.hasMoreTokens()
                    && stringToken.nextToken().trim().equalsIgnoreCase(
                            testBot.getLocalPlayer().getName())) {
                if (!person.isEnemyOf(testBot.getLocalPlayer())) {
                    if (stringToken.hasMoreTokens()) {
                        final String command = stringToken.nextToken().trim(); // NOPMD by Adj1031 on 5/18/14 11:51 AM
                        boolean understood = false;
                        // should create a command factory and a
                        // command object...
                        if (command.equalsIgnoreCase("echo")) { //$NON-NLS-1$
                            understood = true;
                        }
                        if (command.equalsIgnoreCase("calm down")) { //$NON-NLS-1$
                            Iterator<Entity> iteratorEntity = testBot.getEntitiesOwned() // NOPMD by Adj1031 on 5/18/14 11:52 AM
                                    .iterator();
                            while (iteratorEntity.hasNext()) {
                                CEntity cen = testBot.centities.get(iteratorEntity.next()); // NOPMD by Adj1031 on 5/18/14 11:53 AM
                                if (cen.strategy.attack > 1) {
                                    cen.strategy.attack = 1;
                                }
                            }
                            understood = true;
                        } else if (command.equalsIgnoreCase("be aggressive")) { //$NON-NLS-1$
                            Iterator<Entity> iteratorEntity = testBot.getEntitiesOwned() // NOPMD by Adj1031 on 5/18/14 11:53 AM
                                    .iterator();
                            while (iteratorEntity.hasNext()) {
                                final CEntity cen = testBot.centities.get(iteratorEntity.next()); // NOPMD by Adj1031 on 5/18/14 11:55 AM
                                cen.strategy.attack = Math.min(
                                        cen.strategy.attack * 1.2, 1.5);
                            }
                            understood = true;
                        } else if (command.equalsIgnoreCase("attack")) { //$NON-NLS-1$
                            int numOne = Integer.parseInt(stringToken.nextToken().trim()); // NOPMD by Adj1031 on 5/18/14 11:54 AM
                            int numTwo = Integer.parseInt(stringToken.nextToken().trim()); // NOPMD by Adj1031 on 5/18/14 11:55 AM
                            Entity entityTestBot = testBot.game.getFirstEntity(new Coords( // NOPMD by Adj1031 on 5/18/14 11:55 AM
                                    numOne - 1, numTwo - 1));
                            if (entityTestBot != null && entityTestBot.isEnemyOf(testBot.getEntitiesOwned().get(0))) {
                                    CEntity cen = testBot.centities.get(entityTestBot);
                                    cen.strategy.target += 3;
                                  //  System.out.println(cen.entity.getShortName() + " " + cen.strategy.target); //$NON-NLS-1$
                                    understood = true;
                                }
                            }
                        if (understood){
                            testBot.sendChat("Understood " + person.getName());}
                    }
                } else {
                    testBot.sendChat("I can't do that, " + person.getName());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
